﻿using System.Collections.Generic;
using System.Web.Mvc;
using NjaNjam.MongoRepository;
using NjamNjam.Model.Entities;
using System.Linq;
using NjamNjam.Web.Models;
using Ponuda = NjamNjam.Model.Entities.Ponuda;

namespace NjamNjam.Controllers
{
    public class HomeController : Controller
    {
        private MongoRepository m_Repository = new MongoRepository();

        public ActionResult Index()
        {
            return View();
        }

        public PartialViewResult VratiVrsteRestorana(double? latituda, double? longituda, double? dist)
        {
            dist = DistancaUKm(dist);
            var vrsteRestorana = m_Repository.VratiVrsteRestoranaUOkolini(latituda, longituda, dist);
            return PartialView("VrsteRestorana", vrsteRestorana);
        }

        public PartialViewResult VratiRestoraneTipa(long id, double? latituda, double? longituda, double? dist)
        {
            dist = DistancaUKm(dist);
            var restorani = m_Repository.VratiRestoraneTipaUOkolini(id, latituda, longituda, dist);
            return PartialView("RestoraniPartialView", restorani);
        }

        public PartialViewResult VratiVrstePonude(double? latituda, double? longituda, double? dist)
        {
            dist = DistancaUKm(dist);
            var ponuda = m_Repository.VratiVrstePonudeUOkolini(latituda, longituda, dist);
            return PartialView("VrstePonudePartialView", ponuda);
        }

        public PartialViewResult VratiPonuduPoVrsti(long id, double? latituda, double? longituda, double? dist)
        {
            dist = DistancaUKm(dist);
            var ponuda = m_Repository.VratiPonuduUOkolini(id, latituda, longituda, dist);
            return PartialView("PonudaPartialView", ponuda);
        }

        public JsonResult UcitajRestoraneZaIzabranuPonudu(long id, double? latituda, double? longituda, double? dist)
        {
            dist = DistancaUKm(dist);
            var restorani = m_Repository.PretraziRestorane(latituda, longituda, dist, null, new Ponuda {Id = id});
            return Json(restorani, JsonRequestBehavior.AllowGet);
        }

        public JsonResult UcitajRestoran(long id)
        {
            var restoran = m_Repository.VratiRestoranPoId(id);
            return Json(restoran, JsonRequestBehavior.AllowGet);
        }

        public PartialViewResult VratiPonuduRestorana(long id)
        {
            var sifarnikVrstaPonuda = m_Repository.VrstePonude().ToDictionary(x => x.Id);
            var ponudaRestorana = m_Repository.VrstePonuduRestorana(id);
            var vrstePonude = ponudaRestorana.SelectMany(x => x.Ponuda.VrstePonude).Distinct();
            var retVal = new List<VrstaSaPonudom>();
            foreach (var vrstaPonude in vrstePonude)
            {
                var vp = new VrstaSaPonudom
                         {
                             Naziv = sifarnikVrstaPonuda[vrstaPonude.Id].Naziv,
                             Ponuda = ponudaRestorana.Where(x => x.Ponuda.VrstePonude.Any(vp1 => vp1.Id == vrstaPonude.Id)).Select(x => new Web.Models.Ponuda
                             {
                                 Naziv = x.Ponuda.Naziv,
                                 Cena = x.Cena,
                                 Opis = x.Ponuda.Opis,
                                 Slika = x.Ponuda.Id + ".png"
                             }).ToList()
                                                                                                 
                         };
                retVal.Add(vp);
            }

            return PartialView("VrstaPonudeSaPonudomList", retVal);
        }

        private static double? DistancaUKm(double? dist)
        {
            if (dist.HasValue)
            {
                dist = dist / 1000;
            }
            return dist;
        }

        public PartialViewResult PrikaziRestoraneZaIzabranuPonudu(long id, double? latituda, double? longituda, double? dist)
        {
            dist = DistancaUKm(dist);
            var restorani = m_Repository.PretraziRestorane(latituda, longituda, dist, null, new Ponuda { Id = id });
            return PartialView("ListaRestoranaPartialView", restorani);
        }
    }
}
